<?php
namespace app\admin\controller;
use app\admin\logic\CommonLogic;
use app\admin\logic\RechargeLogic;

class RechargeTemplateController extends Controller {
    /**
     * note 充值模板列表
     */
    public function index(){
        $get = $this->request->get();
        if($this->request->isAjax()){
            if($get['type'] == 1){
                $list = RechargeLogic::templatelists($get['type']);
            }else{
                $list = RechargeLogic::getRechargeConfig();
            }
            return json([
                'code' =>1,
                'msg'  =>'',
                'data' =>$list,
            ]);
        }
        return $this->fetch();
    }

    /**
     * note 设置充值
     */
    public function setRecharge(){
        $post = $this->request->post();
        if($this->request->isAjax()){
            RechargeLogic::setRecharge($post);
            return success('设置成功');
        }

    }
    /**
     * note 添加充值模板
     */
    public function add(){

        if ($this->request->isAjax()){
            $post = $this->request->post();
            $result = $this->validate($post,'app\admin\validate\RechargeTemplate');
            if ($result === true){
                RechargeLogic::add($post);
                return success('添加成功');
            }
            return error($result);

        }
        return $this->fetch();
    }
    /**
     * note 编辑充值模板
     */
    public function edit($id){
        if ($this->request->isAjax()){
            $post = $this->request->post();
            $result = $this->validate($post,'app\admin\validate\RechargeTemplate');
            if ($result === true){
                RechargeLogic::edit($post);
                return success('修改成功');
            }
            return error($result);
        }
        $info = RechargeLogic::getRechargeTemplate($id);
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * note 删除充值模板
     */
    public function del($id)
    {
        if ($this->request->isAjax()) {
            $result = RechargeLogic::del($id);
            if ($result) {
                return success('删除成功');
            }
            return success('删除失败');
        }
    }

    public function changefields(){
        $table = 'recharge_template';
        $pk_name = 'id';
        $pk_value = $this->request->get('id');

        $field = $this->request->get('field');
        $field_value = $this->request->get('value');
        $result = RechargeLogic::changeTableValue($table,$pk_name,$pk_value,$field,$field_value);
        if($result){
            return success('修改成功');
        }
        return success('修改失败');
    }
}